package so.shanku.essential.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import net.tsz.afinal.json.JsonMap;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import so.shanku.essential.R;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/9/8.
 */
public class UserDiscountAdapter extends MyBaseAdapter {
	private int discountflag = 0;//

	public UserDiscountAdapter(Context context) {
		super(context);
	}

	public UserDiscountAdapter(Context context, List<? extends Map<String, ?>> datas) {
		super(context, datas);
	}

	public int getDiscountflag() {
		return discountflag;
	}

	public void setDiscountflag(int discountflag) {
		this.discountflag = discountflag;
	}

	//	"CouponName", "DiscountTypeName",
//			"StrValidateStart", "StrValidateEnd"
	@Override
	public View getView(int i, View view, ViewGroup viewGroup) {
		ViewHolder viewHolder = null;
		if (view == null) {
			view = layoutInflater.inflate(R.layout.item_user_discount, viewGroup, false);
			viewHolder = new ViewHolder(view);
			view.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) view.getTag();
		}
		JsonMap map = (JsonMap) datas.get(i);
		String discountMoney = map.getStringNoNull("DiscountMoney");

		if (TextUtils.isEmpty(discountMoney) || discountMoney.equals("0")) {
			String discountPercentStr = map.getStringNoNull("DiscountPercent");
			try {
				if ("".equals(discountPercentStr)) {
					discountPercentStr = "0";
				}
				Double price_double = Double.parseDouble(discountPercentStr);
				price_double = price_double / 10;
				DecimalFormat fmt = new DecimalFormat("0.0");
				viewHolder.tv_price.setText("" + fmt.format(price_double));

			} catch (Exception e) {
				e.printStackTrace();
				viewHolder.tv_price.setText(map.getStringNoNull("discountMoney"));
			}

		} else {
			viewHolder.tv_price.setText("" + discountMoney);
		}
		viewHolder.tv_character.setText(map.getStringNoNull("DiscountTypeName"));
		viewHolder.tv_discount_name.setText(map.getStringNoNull("Name"));
		viewHolder.tv_discount_use_time.setText(map.getStringNoNull("PeriodOfValidity"));

		return view;
	}

	private class ViewHolder {
		@ViewInject(R.id.tv_price)
		private TextView tv_price;
		@ViewInject(R.id.tv_character)
		private TextView tv_character;
		@ViewInject(R.id.tv_discount_name)
		private TextView tv_discount_name;
		@ViewInject(R.id.tv_discount_use_time)
		private TextView tv_discount_use_time;

		public ViewHolder(View view) {
			ViewUtils.inject(this, view);
		}
	}

	@Override
	public boolean isEnabled(int position) {
		if (discountflag == 1) {
			return false;
		} else {
			return super.isEnabled(position);
		}

	}
}
